package com.xiaoyu.lanling.router

import `in`.srain.cube.util.StringUtil
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.fragment.app.Fragment
import com.xiaoyu.base.app.App
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.data.UserData
import com.xiaoyu.base.model.User
import com.xiaoyu.base.utils.MMKVUtil
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.feature.board.BoardActivity
import com.xiaoyu.lanling.feature.board.BoardRuleActivity
import com.xiaoyu.lanling.feature.camera.CameraActivity
import com.xiaoyu.lanling.feature.chat.activity.ChatActivity
import com.xiaoyu.lanling.feature.coin.activity.*
import com.xiaoyu.lanling.feature.coin.model.CoinExchangeItem
import com.xiaoyu.lanling.feature.coin.model.CoinProductItem
import com.xiaoyu.lanling.feature.fate.activity.FatePreferenceActivity
import com.xiaoyu.lanling.feature.feedback.activity.FeedbackActivity
import com.xiaoyu.lanling.feature.friend.activity.FriendListActivity
import com.xiaoyu.lanling.feature.guard.activity.GuardListActivity
import com.xiaoyu.lanling.feature.invite.InviteActivity
import com.xiaoyu.lanling.feature.login.activity.LockActivity
import com.xiaoyu.lanling.feature.login.activity.LoginActivity
import com.xiaoyu.lanling.feature.login.activity.LoginCellphoneActivity
import com.xiaoyu.lanling.feature.main.activity.MainActivity
import com.xiaoyu.lanling.feature.mission.activity.MissionListActivity
import com.xiaoyu.lanling.feature.moment.MomentDetailActivity
import com.xiaoyu.lanling.feature.moment.MomentNoticeActivity
import com.xiaoyu.lanling.feature.moment.MyMomentActivity
import com.xiaoyu.lanling.feature.moment.activity.PublishActivity
import com.xiaoyu.lanling.feature.ontv.HowToBeOnTVActivity
import com.xiaoyu.lanling.feature.privilege.PrivilegeSettingActivity
import com.xiaoyu.lanling.feature.profile.activity.UserNameEditActivity
import com.xiaoyu.lanling.feature.profile.activity.UserProfileActivity
import com.xiaoyu.lanling.feature.profile.activity.UserSoliloquyEditActivity
import com.xiaoyu.lanling.feature.profile.activity.UserVoiceRecordActivity
import com.xiaoyu.lanling.feature.register.activity.RegisterActivity
import com.xiaoyu.lanling.feature.report.activity.ReportActivity
import com.xiaoyu.lanling.feature.setting.activity.AboutUsActivity
import com.xiaoyu.lanling.feature.setting.activity.NotificationActivity
import com.xiaoyu.lanling.feature.setting.activity.SettingActivity
import com.xiaoyu.lanling.feature.setting.activity.SettingPrizeActivity
import com.xiaoyu.lanling.feature.unregister.UnregisterActivity
import com.xiaoyu.lanling.feature.user.UserActivity
import com.xiaoyu.lanling.feature.verify.activity.VerifyHumanActivity
import com.xiaoyu.lanling.feature.videocall.activity.VideoCallActivity
import com.xiaoyu.lanling.feature.videomatch.activity.VideoMatchCallActivity
import com.xiaoyu.lanling.feature.videomatch.activity.VideoMatchWaitingActivity
import com.xiaoyu.lanling.feature.vip.activity.VipBuyActivity
import com.xiaoyu.lanling.feature.vip.activity.VipMineActivity
import com.xiaoyu.lanling.feature.vip.activity.VipProductListActivity
import com.xiaoyu.lanling.feature.vip.model.ProductItem
import com.xiaoyu.lanling.feature.visitor.VisitorHistoryActivity
import com.xiaoyu.lanling.feature.voicecall.activity.ChatCallActivity
import com.xiaoyu.lanling.feature.voicematch.activity.VoiceMatchCallActivity
import com.xiaoyu.lanling.feature.voicematch.activity.VoiceMatchWaitingActivity
import com.xiaoyu.lanling.feature.web.activity.WebActivity
import com.xiaoyu.lanling.media.selector.MediaSelectorType
import com.xiaoyu.lanling.widget.photo.MomentPhotoActivity
import com.xiaoyu.lanling.widget.photo.PhotoActivity
import com.xiaoyu.lib_av.datamodel.CallParams
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

class Router private constructor() {

    companion object {
        val instance by lazy { Router() }
    }

    fun gotoApplicationDetailsSettings(fragment: Fragment) {
        val intent = Intent()
        try {
            intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            intent.addCategory(Intent.CATEGORY_DEFAULT)
            intent.data = Uri.parse("package:" + fragment.context?.packageName)
            fragment.startActivityForResult(intent, Constant.RequestCode.LOCATION)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
            intent.action = Settings.ACTION_SETTINGS
            fragment.startActivityForResult(intent, Constant.RequestCode.LOCATION)
        }
    }

    fun gotoApplicationDetailsSettings(activity: Activity) {
        val intent = Intent()
        try {
            intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            intent.addCategory(Intent.CATEGORY_DEFAULT)
            intent.data = Uri.parse("package:" + activity.packageName)
            activity.startActivityForResult(intent, Constant.RequestCode.LOCATION)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
            intent.action = Settings.ACTION_SETTINGS
            activity.startActivityForResult(intent, Constant.RequestCode.LOCATION)
        }
    }

    fun gotoNotificationSettings() {
        val activity = App.getInstance().topActivity ?: return
        val intent = Intent()
        val context: Context = AppContext.getContext()
        try {
            when {
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                    intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                }
                Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
                    intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                    intent.putExtra("app_package", context.packageName)
                    intent.putExtra("app_uid", context.applicationInfo.uid)
                }
                else -> {
                    intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                    intent.addCategory(Intent.CATEGORY_DEFAULT)
                    intent.data = Uri.parse("package:" + context.packageName)
                }
            }
            activity.startActivity(intent)
        } catch (throwable: Throwable) {
            throwable.printStackTrace()
            intent.action = Settings.ACTION_SETTINGS
            activity.startActivity(intent)
        }
    }

    fun goToWebView(context: Context, url: String?) {
        val intent = Intent(context, WebActivity::class.java)
        intent.putExtra(Constant.PARAM_URL, url)
        context.startActivity(intent)
    }

    fun simplyGoTo(context: Context?, cls: Class<*>?) {
        val intent = Intent(context, cls)
        context?.startActivity(intent)
    }

    fun dispatchAfterLaunch(activity: Activity) {
        val hasProfile = MMKVUtil.get(UserData.getInstance().uid).getBoolean(Constant.MMKVKey.KEY_HAS_PROFILE, false)
        if (UserData.getInstance().hasLogin() && hasProfile) {
            gotoMainActivityForNew(activity, -1)
        } else {
            gotoLoginActivity()
        }
    }

    fun gotoMainActivityForNew(activity: Activity, pageIndex: Int) {
        val intent = Intent(activity, MainActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_MAIN_PAGE_INDEX, pageIndex)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        activity.startActivity(intent)
    }

    fun gotoLoginActivity() {
        val activity: Activity = App.getInstance().topActivity ?: return
        if (activity is LoginActivity) {
            return
        }
        val context: Context = App.getInstance().application
        val intent = Intent(context, LoginActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    fun gotoLoginCellphoneActivity(activity: Activity) {
        simplyGoTo(activity, LoginCellphoneActivity::class.java)
    }

    fun gotoRegisterActivity(activity: Context, name: String = "", loginType: String) {
        val intent = Intent(activity, RegisterActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_NAME, name)
        intent.putExtra(Constant.IntentKey.KEY_LOGIN_TYPE, loginType)
        activity.startActivity(intent)
    }

    fun gotoChatActivityByMessageId(uid: String?, chatId: String?, messageId: String?, fromType: String?) {
        App.getInstance().topActivity?.let {
            val intent = Intent(it, ChatActivity::class.java)
            intent.putExtra(Constant.PARAM_UID, uid)
            intent.putExtra(Constant.PARAM_CHAT_ID, chatId)
            intent.putExtra(Constant.PARAM_MESSAGE_ID, messageId)
            intent.putExtra(Constant.PARAM_FROM, fromType)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            it.startActivity(intent)
        }
    }

    fun gotoChatActivity(uid: String?, chatId: String?, fromType: String?) {
        gotoChatActivityByMessageId(uid, chatId, "", fromType)
    }

    fun gotoUserNameEditActivity(activity: Activity) {
        simplyGoTo(activity, UserNameEditActivity::class.java)
    }

    fun gotoUserSoliloquyEditActivity(activity: Activity, soliloquy: String?) {
        val intent = Intent(activity, UserSoliloquyEditActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_USER_PROFILE_SOLILOQUY, soliloquy)
        activity.startActivity(intent)
    }

    fun gotoCoinProductListAndBalanceActivity(activity: Activity) {
        simplyGoTo(activity, CoinProductListAndBalanceActivity::class.java)
    }

    fun gotoCoinChargeActivity(activity: Activity, productItem: CoinProductItem) {
        val intent = Intent(activity, CoinChargeActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_COIN_PRODUCT_ITEM, productItem)
        activity.startActivity(intent)
    }

    fun gotoCoinIncomeActivity(activity: Activity) {
        simplyGoTo(activity, CoinIncomeActivity::class.java)
    }

    fun gotoUserActivity(activity: Activity, user: User) {
        val intent = Intent(activity, UserActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_USER, user as? Serializable)
        activity.startActivity(intent)
    }

    fun gotoUserProfileEditActivity(activity: Activity, user: User) {
        val intent = Intent(activity, UserProfileActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_USER, user as? Serializable)
        activity.startActivity(intent)
    }

    fun gotoVerifyHumanActivity(activity: Activity) {
        simplyGoTo(activity, VerifyHumanActivity::class.java)
    }

    fun gotoFatePreferenceActivity(activity: Activity) {
        simplyGoTo(activity, FatePreferenceActivity::class.java)
    }

    fun gotoSettingActivity(activity: Activity) {
        simplyGoTo(activity, SettingActivity::class.java)
    }

    fun gotoNotificationActivity(activity: Activity) {
        simplyGoTo(activity, NotificationActivity::class.java)
    }

    fun gotoMissionListActivity(activity: Activity) {
        simplyGoTo(activity, MissionListActivity::class.java)
    }

    fun gotoPhotoActivity(context: Context, selectedImageUrl: String, imageUrlList: ArrayList<String>) {
        val intent = Intent(context, PhotoActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_PHOTO_SELECTED_IMAGE_URL, selectedImageUrl)
        intent.putExtra(Constant.IntentKey.KEY_PHOTO_IMAGE_URL_LIST, imageUrlList)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    fun gotoCoinExchangeListActivity(activity: Activity) {
        simplyGoTo(activity, CoinExchangeListActivity::class.java)
    }

    fun gotoCoinExchangeDetailActivity(activity: Activity, item: CoinExchangeItem) {
        val intent = Intent(activity, CoinExchangeDetailActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_COIN_EXCHANGE_ITEM, item)
        activity.startActivity(intent)
    }

    fun gotoCoinExchangeSuccessActivity(activity: Activity) {
        simplyGoTo(activity, CoinExchangeSuccessActivity::class.java)
    }

    fun gotoFriendListActivity(activity: Activity) {
        simplyGoTo(activity, FriendListActivity::class.java)
    }

    fun gotoUserVoiceRecordActivity(activity: Activity) {
        simplyGoTo(activity, UserVoiceRecordActivity::class.java)
    }

    fun gotoAboutUsActivity(activity: Activity) {
        simplyGoTo(activity, AboutUsActivity::class.java)
    }

    fun gotoFeedbackActivity(activity: Activity, type: String) {
        val intent = Intent(activity, FeedbackActivity::class.java)
        intent.putExtra(Constant.PARAM_TYPE, type)
        activity.startActivity(intent)
    }

    fun gotoLockActivity(activity: Activity, cellphone: String = "", wehchatOpenId: String = "") {
        val intent = Intent(activity, LockActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_LOGIN_CELLPHONE, cellphone)
        intent.putExtra(Constant.IntentKey.KEY_LOGIN_OPEN_ID, wehchatOpenId)
        activity.startActivity(intent)
    }

    fun gotoReportActivity(context: Context, type: String?, objectId: String?, reason: String?) {
        val intent = Intent(context, ReportActivity::class.java)
        intent.putExtra(Constant.PARAM_TYPE, type)
        intent.putExtra(Constant.IntentKey.KEY_REPORT_OBJECT_ID, objectId)
        intent.putExtra(Constant.IntentKey.KEY_REPORT_REASON, reason)
        context.startActivity(intent)
    }

    fun gotoUnregisterActivity(activity: Activity) {
        simplyGoTo(activity, UnregisterActivity::class.java)
    }

    fun gotoBoardActivity(activity: Activity) {
        simplyGoTo(activity, BoardActivity::class.java)
    }

    fun gotoChatCallActivity(context: Context, uid: String, fromType: String, isCaller: Boolean) {
        val intent = Intent(context, ChatCallActivity::class.java)
        intent.putExtra(Constant.PARAM_UID, uid)
        intent.putExtra(Constant.PARAM_FROM, fromType)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_IS_CALLER, isCaller)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        context.startActivity(intent)
    }

    fun gotoVideoCallActivity(context: Context, callParams: CallParams, fromType: String, isCaller: Boolean) {
        val intent = Intent(context, VideoCallActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_CALL_PARAM, callParams)
        intent.putExtra(Constant.PARAM_FROM, fromType)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_IS_CALLER, isCaller)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        context.startActivity(intent)
    }

    fun gotoVipMineActivity(context: Context) {
        simplyGoTo(context, VipMineActivity::class.java)
    }

    fun gotoVipProductListActivity(context: Context, from: String) {
        val intent = Intent(context, VipProductListActivity::class.java)
        intent.putExtra(Constant.PARAM_FROM, from)
        context.startActivity(intent)
    }

    fun gotoVipBuyActivity(activity: Activity, productItem: ProductItem, from: String) {
        val intent = Intent(activity, VipBuyActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_VIP_PRODUCT_SELECTED, productItem)
        intent.putExtra(Constant.PARAM_FROM, from)
        activity.startActivity(intent)
    }

    fun gotoPrivilegeSetting(activity: Activity) {
        simplyGoTo(activity, PrivilegeSettingActivity::class.java)
    }

    fun gotoBoardRuleActivity(activity: Activity) {
        simplyGoTo(activity, BoardRuleActivity::class.java)
    }

    fun gotoVisitHistoryActivity(activity: Activity, visitorCount: Int) {
        val intent = Intent(activity, VisitorHistoryActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_VISITOR_COUNT, visitorCount)
        activity.startActivity(intent)
    }

    fun gotoPublishActivity(activity: Activity?, @MediaSelectorType.MediaSelectorTypeDef type: String = StringUtil.blank(), images: List<String> = Collections.emptyList(), original: Boolean = false) {
        val intent = Intent(activity, PublishActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_MOMENT_MEDIA_URL_LIST, ArrayList(images))
        intent.putExtra(Constant.PARAM_TYPE, type)
        intent.putExtra(Constant.IntentKey.KEY_PHOTO_ORIGINAL, original)
        activity?.startActivity(intent)
    }

    fun gotoMomentPhotoActivity(context: Context, imageUrl: String, imageUrlList: ArrayList<String>, feedId: String) {
        val intent = Intent(context, MomentPhotoActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_PHOTO_SELECTED_IMAGE_URL, imageUrl)
        intent.putExtra(Constant.IntentKey.KEY_PHOTO_IMAGE_URL_LIST, imageUrlList)
        intent.putExtra(Constant.IntentKey.KEY_FEED_ID, feedId)
        context.startActivity(intent)
    }

    fun gotoMyMomentActivity(activity: Activity, user: User) {
        val intent = Intent(activity, MyMomentActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_USER, user)
        activity.startActivity(intent)
    }

    fun gotoMomentDetailActivity(activity: Activity?, feedId: String) {
        val intent = Intent(activity, MomentDetailActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_FEED_ID, feedId)
        activity?.startActivity(intent)
    }

    fun gotoMomentRemindActivity(activity: Activity?, pageIndex: Int) {
        val intent = Intent(activity, MomentNoticeActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_NOTICE_PAGE_INDEX, pageIndex)
        activity?.startActivity(intent)
    }

    fun gotoVoiceMatchWaitingActivity(activity: Activity?) {
        simplyGoTo(activity, VoiceMatchWaitingActivity::class.java)
    }

    fun gotoVoiceMatchCallActivity(context: Context?, params: CallParams, isCaller: Boolean) {
        val intent = createVoiceMatchCallVoiceIntent(context, params, isCaller)
        context?.startActivity(intent)
    }

    fun gotoVideoMatchWaitingActivity(activity: Activity?) {
        simplyGoTo(activity, VideoMatchWaitingActivity::class.java)
    }

    fun gotoVideoMatchCallActivity(activity: Activity?, callParams: CallParams, isCaller: Boolean) {
        activity?.startActivity(createVideoMatchCallVideoIntent(activity, callParams, isCaller))
    }

    fun createVoiceMatchCallVoiceIntent(context: Context?, callParams: CallParams, isCaller: Boolean): Intent {
        val intent = Intent(context, VoiceMatchCallActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_CALL_PARAM, callParams)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_IS_CALLER, isCaller)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        return intent
    }

    fun createVideoMatchCallVideoIntent(context: Context?, callParams: CallParams, isCaller: Boolean): Intent {
        val intent = Intent(context, VideoMatchCallActivity::class.java)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_CALL_PARAM, callParams)
        intent.putExtra(Constant.IntentKey.KEY_CHAT_CALL_IS_CALLER, isCaller)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        return intent
    }

    fun gotoInviteActivity(activity: Activity) {
        simplyGoTo(activity, InviteActivity::class.java)
    }

    fun gotoCameraActivity(activity: Activity) {
        simplyGoTo(activity, CameraActivity::class.java)
    }

    fun gotoGuardListActivity(activity: Activity) {
        simplyGoTo(activity, GuardListActivity::class.java)
    }

    fun gotoSettingPrizeActivity(activity: Activity) {
        simplyGoTo(activity, SettingPrizeActivity::class.java)
    }

    fun gotoOnTV(activity: Activity) {
        simplyGoTo(activity, HowToBeOnTVActivity::class.java)
    }
}